package com.xt.wikiperfumapi.util;

import jakarta.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Component;

@Component
public class ipTools {
    public String  getTools(HttpServletRequest request){
        String ip = request.getHeader("X-Forwarded-For");

        // 如果代理服务器头没有真实 IP 地址，使用 RemoteAddr 获取 IP 地址
        if (ip == null || ip.isEmpty() || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.isEmpty() || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.isEmpty() || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();  // 默认获取客户端的 IP 地址
        }

        // 如果存在多个代理，X-Forwarded-For 头可能是一个逗号分隔的 IP 地址列表
        if (ip != null && ip.contains(",")) {
            ip = ip.split(",")[0];  // 获取第一个 IP 地址，即客户端的真实 IP
        }

        return ip;
    }
}
